package cn.com.w.spring.thread.pool.threadpool;

import cn.com.w.spring.thread.pool.config.ThreadPoolConfigProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 通过properties类型配置文件配置的线程池
 */
@Configuration
public class ThreadPoolFactory {

    private final ThreadPoolConfigProperties threadPoolConfigProperties;

    @Autowired
    public ThreadPoolFactory(ThreadPoolConfigProperties threadPoolConfigProperties) {
        System.out.println("执行了有参构造");
        this.threadPoolConfigProperties = threadPoolConfigProperties;
    }

    @Bean(name = "springFrameworkExector")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(threadPoolConfigProperties.getCorePoolSize());
        executor.setMaxPoolSize(threadPoolConfigProperties.getMaxPoolSize());
        executor.setQueueCapacity(threadPoolConfigProperties.getQueueCapacity());
        executor.setKeepAliveSeconds(threadPoolConfigProperties.getKeepAliveSeconds());
        executor.setThreadNamePrefix("自定义线程-");
        return executor;
    }

}